package io.terminus.lego.shinda.web.controller

import io.swagger.annotations.Api
import io.swagger.annotations.ApiOperation
import org.springframework.security.authentication.AbstractAuthenticationToken
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.PathVariable
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController
import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletResponse

/**
 * @author wangmeng
 * @date 2018/4/16
 */
@Api(value = "绑定服务", description = "绑定社交账户、手机、邮箱")
@RestController
@RequestMapping("/api/bind")
class BindingController constructor() {
    companion object {
        val BIND_SESSION_KEY_REDIRECT = "UC_BINDING_REDIRECT"
        val BIND_SESSION_KEY_USER_ID = "UC_BINDING_USER_ID"
        val BIND_SESSION_KEY_TARGET = "UC_BINDING_TARGET"
    }

    @ApiOperation("绑定第三方账户接口")
    @GetMapping("/social/{target}")
    fun bind(@PathVariable target: String, redirectUrl: String?, auth: AbstractAuthenticationToken, request: HttpServletRequest, response: HttpServletResponse) {
        request.session.setAttribute(BIND_SESSION_KEY_REDIRECT, redirectUrl)
        request.session.setAttribute(BIND_SESSION_KEY_USER_ID, auth.name)
        request.session.setAttribute(BIND_SESSION_KEY_TARGET, target)
        response.sendRedirect("/oauth2/authorization/" + target)
    }

    @GetMapping("/test")
    fun bind(): String {
        return "success"
    }
}